package com.keson.door2.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Constant {
    public static String broker;
    public static String clientNo;
    public static String onlineTopic  = "online";;
    public static String offlineMsg;
    public static int onlineQos = 2;
    public static String actionTopic  = "action";
    public static String typeId;
    public static String onlineMsg;
    public static int actionQos = 2;
    public static String returnTopic = "return";
    public static String url;
    public static boolean enter;

    private static JSONObject config;
    private static JSONObject info;
    private static String configFileName = Environment.getExternalStorageDirectory().toString()+"/config.properties";
    private static String infoFileName = Environment.getExternalStorageDirectory().toString()+"/info.properties";
    private static String appName;
    public static String orgId;
    private static boolean lock;
    private static String version;
    private static String orgName;
    private static String updateUrl;
    private static Context context;

    public static void readConfig(Context applicationContext) {
        Log.i("配置文件：","读取配置");
        Properties prop = new Properties();
        context = applicationContext;
//        try {
//            String config_file = "config.properties";
//            InputStream in = applicationContext.getAssets().open(config_file);  //打开assets目录下的config.properties文件
//            prop.load( new InputStreamReader(in,"utf-8"));
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        config = JSONObject.parseObject(prop.getProperty("config"));

//        try {
//            String config_file = "info.properties";
//            InputStream in = applicationContext.getAssets().open(config_file);  //打开assets目录下的config.properties文件
//            prop.load( new InputStreamReader(in,"utf-8"));
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        info = JSONObject.parseObject(prop.getProperty("info"));

        File configFile = new File(configFileName);
        File infoFile = new File(infoFileName);
        if(!configFile.exists()){
            configFile.mkdir();
        }
        if(!infoFile.exists()){
            infoFile.mkdir();
        }
        FileReader is = null;
        BufferedReader br = null;

        try {
            is = new FileReader(configFile);
            br = new BufferedReader(is);
            String cfg = "";
            String line = "";
            while((line = br.readLine()) != null) {
                cfg = cfg + line;
            }
            config = JSONObject.parseObject(cfg);

            is = new FileReader(infoFile);
            br = new BufferedReader(is);
            String inf = br.readLine();
            info = JSONObject.parseObject(inf);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Constant.broker = config.getString("broker");
        Constant.appName = config.getString("appName");
        Constant.clientNo = info.getString("clientNo");
        Constant.typeId = info.getString("typeId");
        Constant.orgId = info.getString("orgId");
        Constant.version = config.getString("version");
//        Constant.returnTopic = info.getString("returnTopic");

        Constant.lock = config.getBooleanValue("lock");
        Constant.updateUrl = config.getString("updateUrl");
        Constant.orgName = config.getString("orgName");
        Constant.url  = config.getString("url");
        Constant.enter = config.getBooleanValue("enter");

        Constant.onlineMsg = "{clientNo:'"+Constant.clientNo+"',orgId:'"+Constant.orgId+"',type:"+Constant.typeId+",version:'"+Constant.version+"',msg:'1'}";
        Constant.offlineMsg = "{clientNo:'"+Constant.clientNo+"',msg:'0'}";
    }

    public static void updateConfigToFile(JSONObject config, JSONObject info) {
        Log.i("配置文件：","写入配置");
        FileOutputStream verOS1 = null;
        FileOutputStream verOS2 = null;
        try {
            verOS1 = new FileOutputStream(configFileName);
            verOS1.write(config.toJSONString().getBytes());

            verOS2 = new FileOutputStream(infoFileName);
            verOS2.write(info.toJSONString().getBytes());

        } catch (IOException ex) {
        } finally {
            try {
                verOS1.close();
                verOS2.close();
            } catch (IOException ex1) {
            }
        }
    }
}
